import API from '../../api/movies/api_movies';
import * as echarts from 'echarts'
import options from '../components/option.js'
export default {
	data() {
		return {
			applicationNum: null,
			carouselValue: 0,
			carouselList: [{
				"src": require("@/assets/movies/top_carousel_1.png")
			},
			{
				"src": require("@/assets/movies/top_carousel_2.jpg")
			},
			{
				"src": require("@/assets/movies/top_carousel_3.jpg")
			}],
			scoreList: [{
					"title": "最高分",
					"value": 9.8,
					"img": require("@/assets/movies/score-max-img.jpg")
				},
				{
					"title": "最低分",
					"value": 5.6,
					"img": require("@/assets/movies/score-med-img.jpg")
				},
				{
					"title": "中位数",
					"value": 5.1,
					"img": require("@/assets/movies/score-avg-img.jpg")
				}, 
				{
					"title": "平均分",
					"value": 2.1,
					"img": require("@/assets/movies/score-min-img.jpg")
				},
			],
			ratio: '年',
			year: '',
			country: ['美国', '法国'],
			countryList: ['美国', '法国', '英国', '俄罗斯'],
			category: ['喜剧', '科幻'],
			categoryList: ['喜剧', '科幻', '动作', '惊悚']
		};
	},
	created() {},
	methods: {
		initSearch() {
			let that = this;
			//最高分
			API.getMoviesScore(1).then(
				function(r) {
					if (r && r.code === 200) {
						that.scoreList[0].value=r.content;
					} else {
						that.$Message.error(r.message);
					}
				},
				function(err) {
					that.$Message.error(err.toString());
				}
			);
			//最低分
			API.getMoviesScore(2).then(
				function(r) {
					if (r && r.code === 200) {
						that.scoreList[1].value=r.content;
					} else {
						that.$Message.error(r.message);
					}
				},
				function(err) {
					that.$Message.error(err.toString());
				}
			);
			//中位数
			API.getMoviesScore(3).then(
				function(r) {
					if (r && r.code === 200) {
						that.scoreList[2].value=r.content;
					} else {
						that.$Message.error(r.message);
					}
				},
				function(err) {
					that.$Message.error(err.toString());
				}
			);
			//平均值
			API.getMoviesScore(4).then(
				function(r) {
					if (r && r.code === 200) {
						that.scoreList[3].value=r.content;
					} else {
						that.$Message.error(r.message);
					}
				},
				function(err) {
					that.$Message.error(err.toString());
				}
			);
		},
		initChart1() {
			this.countDateChange()
			
		},

		initChart2() {
			//this.countAreaChange()
			
		},
		initChart3() {
			let that = this
			let chartDom = document.getElementById('chart-3');
			let myChart = echarts.init(chartDom);
			API.getTopActors({}).then(
                function (r) {
                    if (r && r.code === 200) {
                        let Xdate1 = Object.keys(r.content);
                        let seriesData1 = [];
                        Xdate1.forEach(element => {
                            seriesData1.push(r.content[element]);
                        });
						let option = options.getTop(Xdate1, seriesData1);
						option && myChart.setOption(option)
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
                    let xData = ['成龙', '刘德华', '周星驰', '周润发', '吴孟达', '吴君如', 'a', 'b', 'c','d']
					let yData = [61, 57, 47, 40, 39, 37, 35, 33, 31, 28]
					let option = options.getTop(xData, yData);
					option && myChart.setOption(option)
                }
            );
			
		},
		initChart4() {
			// let that = this
		},
		init() {
			this.initSearch()
			this.initChart1()
			this.initChart2()
			this.initChart3()
			this.initChart4()
			
		},

		//获取查询下拉列表
		searchSelectData() {
            let that = this;
            that.categoryList = [];
            that.countryList = [];
            API.getAllTypes({}).then(
                function (r) {
                    if (r && r.code === 200) {
                        that.categoryList = r.content;
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
					that.categoryList = [
                        "家庭",
                        "歌舞",
                        "西部",
                        "荒诞",
                        "同性",
                        "爱情",
                        "古装",
                        "戏曲",
                        "黑色电影",
                        "儿童",
                        "奇幻",
                        "动画",
                        "科幻",
                        "武侠",
                        "脱口秀",
                        "短片",
                        "历史",
                        "悬疑",
                        "运动",
                        "动作",
                        "情色",
                        "音乐",
                        "真人秀",
                        "犯罪",
                        "冒险",
                        "战争",
                        "纪录片",
                        "惊悚",
                        "剧情",
                        "传记",
                        "灾难",
                        "恐怖",
                        "喜剧"
                    ];
                    that.$Message.error(err.toString());
                }
            );
            API.getAllAreas({}).then(
                function (r) {
                    if (r && r.code === 200) {
                        that.countryList = r.content;
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
                    that.countryList = [
                        "约旦",
                        "阿根廷",
                        "丹麦",
                        "韩国",
                        "中国香港",
                        "匈牙利",
                        "爱沙尼亚",
                        "西班牙",
                        "阿富汗",
                        "捷克",
                        "摩纳哥",
                        "美国",
                        "斯洛文尼亚",
                        "捷克斯洛伐克",
                        "波兰",
                        "西德",
                        "比利时",
                        "中国大陆",
                        "土耳其",
                        "墨西哥",
                        "芬兰",
                        "秘鲁",
                        "黎巴嫩",
                        "伊朗",
                        "博茨瓦纳",
                        "卢森堡",
                        "阿联酋",
                        "阿尔及利亚",
                        "瑞典",
                        "法国",
                        "印度尼西亚",
                        "摩洛哥",
                        "俄罗斯",
                        "塞尔维亚",
                        "巴哈马",
                        "白俄罗斯",
                        "塞内加尔",
                        "泰国",
                        "越南",
                        "澳大利亚",
                        "巴西",
                        "希腊",
                        "意大利",
                        "新加坡",
                        "马来西亚",
                        "荷兰",
                        "南斯拉夫",
                        "日本",
                        "瑞士",
                        "中国澳门",
                        "爱尔兰",
                        "塞浦路斯",
                        "葡萄牙",
                        "保加利亚",
                        "苏联",
                        "卡塔尔",
                        "古巴",
                        "马耳他",
                        "英国",
                        "挪威",
                        "南非",
                        "东德",
                        "委内瑞拉",
                        "新西兰",
                        "奥地利",
                        "罗马尼亚",
                        "德国",
                        "中国",
                        "印度",
                        "巴基斯坦",
                        "加拿大",
                        "中国台湾"
                    ];
                    that.$Message.error(err.toString());
                }
            );
        },
		//国家地区分析
		countAreaChange() {
			let that = this;
            API.getCountByArea(that.country).then(
                function (r) {
                    if (r && r.code === 200) {
						let data = []
						let dates = Object.keys(r.content);
						let sum=0;
        				dates.forEach(element => {
               				sum+=r.content[element]
            			});
						dates=dates.slice(-10);
						dates.forEach(element =>{
							data.push({
								name: element,
							 	value: r.content[element]
							 });
							 sum=sum-r.content[element];
						});
						data.push({
							name:"其他",
							value:sum
						})
						
						let chartDom = document.getElementById('chart-2');
						let myChart = echarts.init(chartDom);
						let option = options.getRose(data);
						option && myChart.setOption(option)
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
                    let data = [
                        {
                            name: '情色',
                            value: '3'
                        }, {
                            name: '喜剧',
                            value: '340'
                        }, {
                            name: '科幻',
                            value: '149'
                        }, {
                            name: '运动',
                            value: '13'
                        }, {
                            name: '恐怖',
                            value: '62'
                        }, {
                            name: '儿童',
                            value: '5'
                        }, {
                            name: '犯罪',
                            value: '151'
                        }, {
                            name: '惊悚',
                            value: '182'
                        }, {
                            name: '冒险',
                            value: '216'
                        }, {
                            name: '恐怖',
                            value: '62'
                        }
                    ];
                    let chartDom = document.getElementById('chart-2');
					let myChart = echarts.init(chartDom);
					let option = options.getRose(data);
					option && myChart.setOption(option)
                }
            );
		},

		//类型分析
		countTypeChange() {
			let that = this;
            API.getCountByType(that.category).then(
                function (r) {
                    if (r && r.code === 200) {
						let data = []
						let dates = Object.keys(r.content);
						let sum=0;
        				dates.forEach(element => {
               				sum+=r.content[element]
            			});
						dates=dates.slice(-10);
						dates.forEach(element =>{
							data.push({
								name: element,
							 	value: r.content[element]
							 });
							 sum=sum-r.content[element];
						});
						data.push({
							name:"其他",
							value:sum
						})
						
						let chartDom = document.getElementById('chart-4');
						let myChart = echarts.init(chartDom);
						let option = options.getPie(data);
						option && myChart.setOption(option)
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
                    let data = [{
						name: '美国',
						value: '602'
					}, {
						name: '法国',
						value: '102'
					}, {
						name: '中国香港',
						value: '249'
					}, {
						name: '加拿大',
						value: '49'
					}, {
						name: '中国大陆',
						value: '215'
					}, {
						name: '英国',
						value: '125'
					}, {
						name: '德国',
						value: '35'
					}, {
						name: '日本',
						value: '75'
					}, {
						name: '意大利',
						value: '39'
					}, {
						name: '韩国',
						value: '18'
					}, ];
                    let chartDom = document.getElementById('chart-4');
					let myChart = echarts.init(chartDom);
					let option = options.getPie(data);
					option && myChart.setOption(option)
                }
            );
		},

		//按年查找
		countDateChange() {
			let that = this
			let chartDom = document.getElementById('chart-1');
			let myChart = echarts.init(chartDom);
            API.getCountByYear().then(
                function (r) {
                    if (r && r.code === 200) {
                        let dates = Object.keys(r.content);
                        //dates.sort();
                        let seriesData = [];
						dates.splice(-1,1);
						dates=dates.slice(-10);
                        dates.forEach(element => {
                            seriesData.push(r.content[element]);
                        });
						let xData = dates
						let yData = seriesData
						let maxArr = [200, 200, 200, 200, 200, 200, 200, 200,200,200]
						let option = options.getSingleBar(xData, yData,maxArr);
						option && myChart.setOption(option)
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
                    let xData = [1970, 1971, 1972, 1973, 1974, 1975, 1976, 1979]
					let yData = [23, 28, 33, 35, 30, 24, 28, 42]
					let maxArr = [50, 50, 50, 50, 50, 50, 50, 50]
					let option = options.getSingleBar(xData, yData, maxArr);
					option && myChart.setOption(option)
                    that.$Message.error(err.toString());
                }
            );

			
		},
		//按月查找
		countDateSelectedChange() {
            let that = this;
            let date = new Date(that.year);
            API.getCountByDate(date.getFullYear()).then(
                function (r) {
                    if (r && r.code === 200) {
                        let dates = Object.keys(r.content);
						let countMax=0;
                    	dates.sort();
                        let seriesData = [];
                        dates.forEach(element => {
							let count=r.content[element]
                            seriesData.push(count);
							if(count>countMax) countMax=count;
                        });
						let maxArr = []
						dates.forEach(element => {
							maxArr.push(countMax+2);
                        });

						let chartDom = document.getElementById('chart-1');
						let myChart = echarts.init(chartDom);
						let xData = dates
						let yData = seriesData
						let option = options.getSingleBar(xData, yData, maxArr);
						option && myChart.setOption(option)
                    } else {
                        that.$Message.error(r.message);
                    }
                },
                function (err) {
                    let xData = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
                    let yData = [20, 32, 91, 34, 12, 33, 20, 34, 12, 20, 32, 34];
                    let maxArr = [50, 50, 50, 50, 50, 50, 50, 50]
					let option = options.getSingleBar(xData, yData, maxArr);
					option && myChart.setOption(option)
                    that.$Message.error(err.toString());
                }
            );
        },
		initEchart() {
			// let that = this;
			// let countDateOption = {
			// 	grid: {
			// 		//与绝对定位相似，top，left，right，bottom 设定是根据上级盒子宽高来计算
			// 		top: "5%",
			// 		left: "5%",
			// 		right: "2%",
			// 		bottom: "5%"
			// 	},
			// 	xAxis: {
			// 		type: 'category',
			// 		// boundaryGap: false,
			// 		axisLabel: {
			// 			textStyle: {
			// 				fontSize: 20,
			// 				fontWeight: 'bold',
			// 				color: '#fff'
			// 			},
			// 			formatter: function(value) {
			// 				var texts = value;
			// 				texts += ('月');
			// 				return texts;
			// 			}
			// 		},
			// 		data: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
			// 	},
			// 	yAxis: {
			// 		type: 'value',
			// 		splitLine: {
			// 			show: false
			// 		},
			// 		axisLine: {
			// 			show: true
			// 		},
			// 		axisTick: {
			// 			show: true,
			// 		},
			// 		axisLabel: {
			// 			textStyle: {
			// 				fontSize: 20,
			// 				fontWeight: 'bold',
			// 				color: '#fff'
			// 			},
			// 			formatter: function(value) {
			// 				var texts = value;
			// 				texts += ('部');
			// 				return texts;
			// 			}
			// 		},
			// 	},
			// 	series: [{
			// 		data: [20, 32, 91, 34, 12, 33, 20, 34, 12, 20, 32, 34],
			// 		type: 'line',
			// 		smooth: true,
			// 		areaStyle: {
			// 			color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
			// 					offset: 0,
			// 					color: 'rgba(58,77,233,0.8)'
			// 				},
			// 				{
			// 					offset: 1,
			// 					color: 'rgba(58,77,233,0.3)'
			// 				}
			// 			])
			// 		},
			// 	}]
			// };
			// let countDateEchart = echarts.init(document.getElementById('countDate_echart'));
			// countDateEchart.setOption(countDateOption, true);
		}
	},
	mounted() {
		// this.initSearch();
		// this.initEchart();
		this.searchSelectData()
		this.init()
	}
};